<?
namespace Admin;

use AdminController;
use Config;
use DB;
use Input;
use Lang;
use Redirect;
use Sentry;
use Str;
use Validator;
use View;
use Jinput;
use Lyric;

class LyricsController extends AdminController
{
    public function getIndex()
    {
        $data['appends']['withTrashed'] = Jinput::get('withTrashed', false);
        $data['appends']['onlyTrashed'] = Jinput::get('onlyTrashed', false);
        $data['appends']['tone'] = Jinput::get('tone', 0);
        $data['appends']['category'] = Jinput::get('category', 0);
        $data['appends']['status'] = Jinput::get('status', 'published');

        $myModel = new \MyModel();
        $arrParams = array();
        $arrParams['conditions'][] = array('field' => 'lyrics.status', 'opr' => '=', 'value' => $data['appends']['status']);
        if ($data['appends']['tone'] != 0)
            $arrParams['conditions'][] = array('field' => 'lyrics.tone_id', 'opr' => '=', 'value' => $data['appends']['tone']);
        if ($data['appends']['category'] != 0)
            $arrParams['conditions'][] = array('field' => 'lyrics.category_id', 'opr' => '=', 'value' => $data['appends']['category']);
        $arrParams['left_joins'][] = array('table' => 'tones', 'pk' => 'tones.id', 'fk' => 'tone_id');
        $arrParams['left_joins'][] = array('table' => 'categories', 'pk' => 'categories.id', 'fk' => 'category_id');
        $arrParams['paginate']['appends'] = $data['appends'];
        $arrParams['paginate']['nums'] = 25;
        $arrParams['selects'] = array('lyrics.id', 'lyrics.title', 'lyrics.tone_id', 'lyrics.category_id', 'lyrics.created_at', 'tones.title as tone_title', 'categories.title as category_title');
        $data['lyrics'] = $myModel->listItems('Lyric', $arrParams);
        $data['tones'] = $myModel->listAllItemsToArray("Tone", array('value' => 'id', 'display' => 'title'));
        $data['categories'] = $myModel->listAllItemsToArray("Category", array('value' => 'id', 'display' => 'title'));
        return View::make('backend/lyrics/index', compact('data'));
    }

    /**
     *
     */
    public function getCreate()
    {

    }

    /**
     *
     */
    public function getEdit($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['lyric', 'lyric.edit']))
            return View::make('backend/notallow');
        if (is_null($data['lyric'] = Lyric::find($id)))
            return Redirect::route('lyrics')->with('error', 'Nội dung không tồn lại');
        $data['topics'] = array();
        $data['topicables'] = array();

        $data['topics'] = \Topic::returnArray();

        $topicables = $data['lyric']->topics()->select('topic_id')->get();
        foreach ($topicables as $topicable) {
            $data['topicables'][] = $topicable->topic_id;
        }
        $data['tags'] = $data['lyric']->tags()->select('tags.*')->get();
        $myModel = new \MyModel();
        $data['singer'] = $data['lyric']->singer()->first();
        $data['musician'] = $data['lyric']->musician()->first();
        $data['categories'] = $myModel->listAllItemsToArray('Category', array('value' => 'id', 'display' => 'title'));
        $data['tones'] = $myModel->listAllItemsToArray('Tone', array('value' => 'id', 'display' => 'title'));
        return View::make('backend/lyrics/edit', compact('data'));
    }

    public function postEdit($id)
    {
        $data = Input::get('data');
        $rules = array('title' => 'required', 'content' => 'required|min:30');
        $validation = Validator::make($data['Lyric'], $rules);
        if ($validation->fails()) {
            return Redirect::back()->withInput()->withErrors($validation);
        }

        $myModel = new \MyModel();

        if (!empty($data['Singer']['id']))
            $data['Lyric']['singer_id'] = $data['Singer']['id'];
        else if (!empty($data['Singer']['name'])) {
            $singer_id = $myModel->saveItem('Singer', array('name' => $data['Singer']['name'], 'type' => 'singer'));
            $data['Lyric']['singer_id'] = $singer_id;
        }

        if (!empty($data['Musician']['id']))
            $data['Lyric']['musician_id'] = $data['Musician']['id'];
        else if (!empty($data['Musician']['name'])) {
            $musician_id = $myModel->saveItem('Singer', array('name' => $data['Musician']['name'], 'type' => 'musician'));
            $data['Lyric']['musician_id'] = $musician_id;
        }

        $lyric = $myModel->saveItem('Lyric', $data['Lyric'], array('return' => 'class'));
        if (isset($data['Topics'])) {
            $lyric->topics()->detach();
            $lyric->topics()->attach($data['Topics']);
        }


        if (isset($data['Tags']) && $data['Tags'] != '') {
//            $lyric->tags()->detach();
            $tags = explode(',', $data['Tags']);
            foreach ($tags as $tag) {
                $tag_id = \Tag::Check($tag);
                $lyric->tags()->attach($tag_id);
            }
        }

        return Redirect::route('update/lyric/admin', $id)->with('success', 'Hợp âm đã được lưu');
    }
}
